import {
    Net
} from "../../utils/Net"
import {
    DataBus
} from '../../utils/DataBus'
const net = new Net()
const DB = new DataBus()
// pages/s_detail/01.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        ID: '',
        movie: '',
        pl: [{
            image: '/img/shoucang.png',
            name: '评分'
        }],
        liuyan: '',
        pl_display: 'none',
        choose: false,
        rate: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        // myrate:'评分',
    },
    async _loadData(ID) {
        const one = await net.GetFunc('movieOne', {
            ID: ID
        })
        console.log(ID,one)
        //处理未登录
        if (one.success !== 1 && one.code === 450) {
            wx.redirectTo({
                url: '/pages/s_login/01',
            })
            return //跳到用户登录页面不再运行
        }
        for(let i=0;i<one.msg.comments.length;i++){
            var Time=require('../../utils/util.js')
            one.msg.comments[i].addTime=Time.formatTime(new Date(one.msg.comments[i].addTime),'Y-M-D h:m:s')
            
        }
        this.setData({
            movie: one.msg
        })
    },
    Tapchoose(e) {
        console.log(e)
    },
    Tapinput(e) {
        this.setData({
            liuyan: e.detail.value
        })
    },
    Tappinglun() {
        let display = ''
        if (this.data.choose === false) {
            display = 'flex'
        } else {
            display = 'none'
        }
        this.setData({
            pl_display: display,
            choose: !this.data.choose
        })
    },
    async Taponload() {
        if(this.data.liuyan.length<=8){
            wx.showToast({
              title: '评论字数少于八',
              duration:3000,
              icon:'error'
              
            })
            return
        }
        const comment = {
            ID: this.data.ID,
            content: this.data.liuyan
        }
        console.log(comment)
        const res = await net.Post('addComment', comment)
        this.setData({
            liuyan: ''
        })
        this._loadData(this.data.ID)
        // console.log("什么情况",res)
    },
    async Tapchoose(e) {
        const value = e.detail.value
        const pf = {
            ID: this.data.ID,
            rate: this.data.rate[value]
        }
        const res = await net.Post('movieRate', pf)
        console.log(res)
        if(res.success===1){
            wx.showModal({
              content: '评分成功',
              showCancel:false
            })
            this.setData({rate:res.msg,myrate:this.data.rate[value]})
        }else{
            wx.showModal({
                content: '评分失败',
                showCancel:false
              })
        }
        this._loadData(this.data.ID)
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const ID = options.id
        this.setData({
            ID
        })
        console.log(this.data)
        this._loadData(ID)
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})